package com.woniuxy.rms.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.rms.advice.LogPoint;
import com.woniuxy.rms.config.UploadConfig;
import com.woniuxy.rms.pojo.Roles;
import com.woniuxy.rms.pojo.ServerMenu;
import com.woniuxy.rms.pojo.Servers;
import com.woniuxy.rms.service.ServerService;
import com.woniuxy.rms.util.DataResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Auther: ChenGang  <br/>
 * Date: 2020/3/27 0027:上午 11:15  <br/>
 * Description:服务控制层
 */
@Controller
@RequestMapping("/server")
public class ServerController {

    @Autowired
    private UploadConfig uploadConfig;

    @Autowired
    ServerService serverService;

    @Value("${upfile.path}")
    String loadPath;//文件上传根目录

    @Value("${upfile.imgPath}")
    String imgPath;//图片保存目录
    /**
     * Description : 服务菜单查询  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 上午 11:18 [Administrator]);
     * @return java.util.Map
    */
    @GetMapping("/menuList")
    @ResponseBody
    @LogPoint("查看服务菜单")
    public Map serverMenuList(){
        List<ServerMenu> serverMenus = serverService.findAll();
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","ok");
        map.put("data",serverMenus);
        map.put("count",serverMenus.size());
        return map;
    }
    /**
     * Description : 跳转到添加服务菜单页面  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 1:48 [Administrator]);
     * @param parentId
     * @param model
     * @return org.springframework.web.servlet.ModelAndView
    */
    @GetMapping("/addServerMenu")
    public ModelAndView addServerMenu(Long parentId,ModelAndView model){
        model.setViewName("/server/addServerMenu");
        model.addObject("parentId",parentId);
        return model;
    }
    /**
     * Description : 服务图片上传  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 2:54 [Administrator]);
     * @param file
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/upload")
    @ResponseBody
    @LogPoint("服务图片上传")
    public DataResult upload(MultipartFile file) {
       return uploadConfig.uploadImg(file,loadPath,imgPath+"server");
    }

    /**
     * Description : 添加服务菜单  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 3:07 [Administrator]);
     * @param serverMenu
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/saveMenu")
    @ResponseBody
    @LogPoint("添加服务菜单")
    public DataResult saveMenu(ServerMenu serverMenu){
        return serverService.saveMenu(serverMenu);
    }
    /**
     * Description : 跳转到菜单编辑  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 4:12 [Administrator]);
     * @param id
     * @param modelAndView
     * @return org.springframework.web.servlet.ModelAndView
    */
    @GetMapping("/editMenu")
    public ModelAndView editMenu(Long id,ModelAndView modelAndView){
        //获取菜单数据
        ServerMenu serverMenu = serverService.findById(id);
        modelAndView.addObject("serverMenu",serverMenu);
        modelAndView.setViewName("/server/editServerMenu");
        return modelAndView;
    }
    /**
     * Description : 修改菜单  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 5:05 [Administrator]);
     * @param serverMenu
     * @return com.woniuxy.rms.util.DataResult
    */
    @PostMapping("/updateMenu")
    @ResponseBody
    @LogPoint("修改服务菜单")
    public DataResult updateMenu(ServerMenu serverMenu){
        return serverService.updateMenu(serverMenu);
    }
    /**
     * Description : 批量删除服务  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 6:47 [Administrator]);
     * @param ids
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/batchDel")
    @ResponseBody
    @LogPoint("批量删除服务菜单")
    public DataResult batchDel(String ids){
        return serverService.batchDel(ids);
    }
    /**
     * Description : 审核状态  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 7:50 [Administrator]);
     * @param id
     * @param status
     * @return com.woniuxy.rms.util.DataResult
    */
    @PutMapping("/checkMenu")
    @ResponseBody
    @LogPoint("审核服务")
    public DataResult checkMenu(Long id,Integer status){
        return serverService.checkMenu(id,status);
    }
    /**
     * Description : 获取入驻列表  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 8:33 [Administrator]);
     * @param currentPage
     * @param pageSize
     * @return com.woniuxy.rms.util.DataResult
    */
    @GetMapping("/enterList")
    @ResponseBody
    @LogPoint("查看入驻列表")
    public DataResult enterList(@RequestParam(defaultValue = "1",required = false,name = "page") Integer currentPage,
                                @RequestParam(defaultValue = "10",required = false,name = "limit") Integer pageSize){
            List<Servers> services = serverService.findByPage(currentPage,pageSize);
            PageInfo info = new PageInfo(services);
            return DataResult.success(info);
    }
    /**
     * Description : 入驻审核  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 9:25 [Administrator]);
     * @param id
     * @param status
     * @return com.woniuxy.rms.util.DataResult
    */
    @PutMapping("/checkStatus")
    @ResponseBody
    @LogPoint("入驻审核")
    public DataResult checkStatus(Long id,Integer status){
        return serverService.checkStatus(id,status);
    }

    /**
     * Description : 批量删除入驻用户  <br/>
     * ChangeLog : 1. 创建 (2020/3/27 0027 下午 9:28 [Administrator]);
     * @param ids
     * @return com.woniuxy.rms.util.DataResult
    */
    @DeleteMapping("/batchDelEnter")
    @ResponseBody
    @LogPoint("批量删除入驻用户")
    public DataResult batchDelEnter(String ids){
        return serverService.batchDelEnter(ids);
    }
}